﻿using Budget.Config;
using Budget.Core.Data;
using Budget.Data;
using Budget.Data.Configs;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using NHibernate;

namespace Budget.Locator.Installers
{
    public class DatabaseInstaller : IWindsorInstaller
    {
        public DatabaseInstaller()
        {
            var connection = BudgetConfig.BudgetConnectionString;

            DB.Init(new SqlConfig(connection));
        }

        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component.For<ISessionFactory>().Instance(DB.Factory).LifeStyle.Singleton);
            container.Register(Component.For<ISession>().UsingFactoryMethod(x => x.Resolve<ISessionFactory>().OpenSession()).LifeStyle.PerWcfOperation());
            container.Register(Component.For<INHibernateTransaction>().ImplementedBy<NHibernateTransaction>().LifeStyle.PerWcfOperation());
            container.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(Repository<>)).LifeStyle.Transient);
        }
    }
}